package no.ntnu.item.ttm4115.termproject.playeragent.gui;





/****************************************************************/
/*                      LoginDialog	                            */
/*                                                              */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;





import no.ntnu.item.ttm4115.termproject.Login;

import no.ntnu.item.ttm4115.termproject.UserLogin;
import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;

/**
 * Player logging in
 *
 */

@SuppressWarnings("serial")
public class LoginDialog  extends javax.swing.JFrame
{
	// Variables declaration
	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public JLabel imageLabel;
	private JLabel JLabel1;
	private JLabel jLabel2;
	private JPasswordField jPasswordField1;
	private JTextField jTextFieldUsername;
	private JButton jButton1;
	private JPanel contentPane;
	//-----
	private JPanel jPanel4;
	//-----
	// End of variables declaration
	private ActorSM asm;
	public MeetingPlaceGUI guiRef;
	
	private AbstractAction abstractActionLogin;

	public LoginDialog(ActorSM asm)
	{
		super();
		
		this.setVisible(true);
		this.asm = asm;
		this.setLocationRelativeTo(null);
		initGUI();
	}

	/**
	 * Initialize the GUI
	 * Don't change this method
	 */
	private void initGUI()
	{
		
		
		
		JLabel1 = new JLabel();
		jLabel2 = new JLabel();
		jPasswordField1 = new JPasswordField();
		jTextFieldUsername = new JTextField();
		jButton1 = new JButton();
		contentPane = (JPanel)this.getContentPane();
		//-----
		jPanel4 = new JPanel();
		//-----
		//ImageIcon background = createImageIcon("treasure.gif", "TreasurePic");
		//imageLabel = new JLabel("", background, JLabel.CENTER);
		
		imageLabel = new JLabel();
		imageLabel.setIcon(new ImageIcon(getClass().getResource("callforhelp.gif")));
		//
		// USername Label
		//
		
		JLabel1.setText("Username");
		//
		// Password Label
		//
		jLabel2.setText("Password");
		//
		//	Login Button
		//
		jButton1.setText("Login");
		jButton1.setAction(getAbstractActionLogin());
		
		
		//
		// contentPane
		//
		contentPane.setLayout(null);
		contentPane.setBackground(new Color(254, 253, 252));
		addComponent(contentPane, JLabel1, 100,205,68,21);
		addComponent(contentPane, jLabel2, 99,239,77,23);
		addComponent(contentPane, jPasswordField1, 206,239,165,22);
		addComponent(contentPane, jTextFieldUsername, 207,207,164,22);
		addComponent(contentPane, jButton1, 290,278,83,28);
		addComponent(contentPane, jPanel4, 97,12,275,179);
		//
		// jPanel4
		//
		jPanel4.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		jPanel4.setBackground(new Color(254, 253, 250));
		
		
		
		jPanel4.add(imageLabel);

		//jPanel4.setIcon(new ImageIcon(getClass().getResource("trondheim.gif")));
		

		
		
		//
		// LoginDialog
		//
		this.setTitle("Call for Help - Login");
		this.setLocation(new Point(103, 155));
		this.setSize(new Dimension(471, 359));
	}

	/** Add Component Without a Layout Manager (Absolute Positioning) */
	private void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}

	//
	// TODO: Add any appropriate code in the following Event Handling Methods
	//
	

	


	protected static ImageIcon createImageIcon(String filbanen, String beskrivelse) {
		java.net.URL imgURL = Label.class.getResource(filbanen);
		if (imgURL != null) {
			return new ImageIcon(imgURL, beskrivelse);
		} else {
			System.err.println("Could not find file: " + filbanen);
			return null;
		}
	}


	/**
	 * This method will be called when the Player agent receives the LoginSuccessful signal from ClassManager
	 * @param signal
	 */
	public void loginSuccessful(Login signal){
		
		jTextFieldUsername.setText("");
		disposeMeShowMain(signal);
	}
	
	public MeetingPlaceGUI getGuiRef() {
		return this.guiRef;
	}
	
	/**
	 * Don't change this method
	 * @param signal
	 */
	private void disposeMeShowMain(Login signal){
		
		//this.dispose();
		this.setVisible(false);
		this.guiRef = new MeetingPlaceGUI(getAsm(), signal);
		
		
	}
	
	public JTextField getJTextFieldUsername() {
		return jTextFieldUsername;
	}
	
	public ActorSM getAsm() {
		return asm;
	}

	public void setAsm(ActorSM asm) {
		this.asm = asm;
	}
	
	
	/**
	 * When the login button is clicked
	 * @return
	 */
	public AbstractAction getAbstractActionLogin() {
		if(abstractActionLogin == null) {
			abstractActionLogin = new AbstractAction("Login", null) {
				public void actionPerformed(ActionEvent evt) {
					
					System.out.println("Login button pressed");
					
					getAsm().sendMessage(new UserLogin(getJTextFieldUsername().getText().trim()), new ActorAddress("/classManager", "ClassManager"));

				}
			};
		}
		return abstractActionLogin;
	}



}
